#include "com_anlogic_sdk_tools_nativetool_NativeBspTool.h"
#include "config.hpp"
#include "dr1x90_tool.hpp"
#include "ph1p35_tool.hpp"

#include <stdio.h>
#include <fcntl.h>
#include <io.h>

bsp_tool* bspTool = NULL;
dr1x90_tool* dr1x90Tool = new dr1x90_tool();
ph1p35_tool* ph1p35Tool = new ph1p35_tool();

#define MAXBUF 20480
static int oldstdout;
static int pipehandles[2];
static char buf[MAXBUF];

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    redirectStdoutInit
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_redirectStdoutInit(JNIEnv* env, jobject obj)
{
    oldstdout = _dup(_fileno(stdout));
    _pipe(pipehandles, MAXBUF, _O_BINARY);
    _dup2(pipehandles[1], _fileno(stdout));
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    redirectStdoutDone
 * Signature: ()V
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_redirectStdoutDone(JNIEnv* env, jobject obj)
{
    fflush(stdout);
    _dup2(oldstdout, _fileno(stdout));
    int n = _read(pipehandles[0], buf, sizeof(buf));
	buf[n] = '\0';
    return env->NewStringUTF(buf);
}

std::string get_func_name(std::string full_name) {
	return std::string(full_name, full_name.rfind('_') + 1);
}

bsp_tool* get_bsp_tool(JNIEnv* env, jobject obj)
{
	bspTool = NULL;

	jclass cls = env->GetObjectClass(obj);

	jmethodID methodId = env->GetMethodID(cls, "getChip", "()Ljava/lang/String;");
	
	if (NULL == methodId)
		return NULL;

	jstring chip_name_jstr = (jstring)env->CallObjectMethod(obj, methodId);

	const char *chip_name = env->GetStringUTFChars(chip_name_jstr, NULL);
	
	if (NULL == chip_name)
		return NULL;

	if (strcmp(chip_name, CHIP_DR1M90) == 0 || strcmp(chip_name, CHIP_DR1V90) == 0) {
		bspTool = dr1x90Tool;
	} else if (strcmp(chip_name, CHIP_PH1P35) == 0) {
		bspTool = ph1p35Tool;
	} else {
		bspTool = NULL;
	}

	env->ReleaseStringUTFChars(chip_name_jstr, chip_name);

	return bspTool;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    createDefaultMss
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_createDefaultMss
  (JNIEnv* env, jobject obj, jstring mssfile, jstring chip, jstring proc, jstring os)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return;
	const char* chip_cstr = env->GetStringUTFChars(chip, NULL);
	if (NULL == chip_cstr) return;
	const char* proc_cstr = env->GetStringUTFChars(proc, NULL);
	if (NULL == proc_cstr) return;
	const char* os_cstr = env->GetStringUTFChars(os, NULL);
	if (NULL == os_cstr) return;

	try {
		bspTool->create_default_mss(mssfile_cstr, chip_cstr, proc_cstr, os_cstr);
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);
	env->ReleaseStringUTFChars(chip, chip_cstr);
	env->ReleaseStringUTFChars(proc, proc_cstr);
	env->ReleaseStringUTFChars(os, os_cstr);
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    createMssFromHpf
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_createMssFromHpf
  (JNIEnv* env, jobject obj, jstring hpf, jstring mssfile, jstring chip, jstring proc, jstring os)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return;

	const char* hpf_cstr = env->GetStringUTFChars(hpf, NULL);
	if (NULL == hpf_cstr) return;
	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return;
	const char* chip_cstr = env->GetStringUTFChars(chip, NULL);
	if (NULL == chip_cstr) return;
	const char* proc_cstr = env->GetStringUTFChars(proc, NULL);
	if (NULL == proc_cstr) return;
	const char* os_cstr = env->GetStringUTFChars(os, NULL);
	if (NULL == os_cstr) return;

	try {
		bspTool->create_mss_from_hpf(hpf_cstr, mssfile_cstr, chip_cstr, proc_cstr, os_cstr);
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(hpf, hpf_cstr);
	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);
	env->ReleaseStringUTFChars(chip, chip_cstr);
	env->ReleaseStringUTFChars(proc, proc_cstr);
	env->ReleaseStringUTFChars(os, os_cstr);
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getProcessor
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getProcessor
  (JNIEnv* env, jobject obj, jstring mssfile)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return NULL;

    jstring ret = NULL;

	try {
		std::string proc = bspTool->get_processor(mssfile_cstr);
		if (proc.empty()) return NULL;
		ret = env->NewStringUTF(proc.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);

	return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getOsInfo
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getOsInfo
  (JNIEnv* env, jobject obj, jstring mssfile)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return NULL;

    jstring ret = NULL;

	try {
		nlohmann::json doc = bspTool->get_os(mssfile_cstr);
		if (NULL == doc) return NULL;
		ret = env->NewStringUTF(doc.dump().c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);

	return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getDocInfo
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getDocInfo
  (JNIEnv* env, jobject obj, jstring mssfile)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return NULL;

	jstring ret = NULL;

	try {
		nlohmann::json doc = bspTool->get_doc(mssfile_cstr);
		if (NULL == doc) return NULL;
		ret = env->NewStringUTF(doc.dump().c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);
	
	return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getDriverList
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getDriverList
  (JNIEnv* env, jobject obj, jstring mssfile)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return NULL;

	jstring ret = NULL;

	try {
		nlohmann::json doc = bspTool->get_drivers(mssfile_cstr);
		if (NULL == doc) return NULL;
		ret = env->NewStringUTF(doc.dump().c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);

	return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getDriver
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getDriver
  (JNIEnv* env, jobject obj, jstring mssfile, jstring name)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return NULL;
	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return NULL;

	jstring ret = NULL;

	try {
		nlohmann::json doc = bspTool->get_driver(mssfile_cstr, name_cstr);
		if (NULL == doc) return NULL;
		ret = env->NewStringUTF(doc.dump().c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);
	env->ReleaseStringUTFChars(name, name_cstr);

	return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    removeDriver
 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_removeDriver
  (JNIEnv* env, jobject obj, jstring mssfile, jstring name)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return;
	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return;

	try {
		bspTool->delete_driver(mssfile_cstr, name_cstr);
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);
	env->ReleaseStringUTFChars(name, name_cstr);
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    addDriver
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_addDriver
  (JNIEnv* env, jobject obj, jstring mssfile, jstring name, jstring version)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return;
	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return;
	const char* version_cstr = env->GetStringUTFChars(version, NULL);
	if (NULL == version_cstr) return;

	try {
		bspTool->add_driver(mssfile_cstr, name_cstr, version_cstr);
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);
	env->ReleaseStringUTFChars(name, name_cstr);
	env->ReleaseStringUTFChars(version, version_cstr);
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    setDriverVersion
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_setDriverVersion
  (JNIEnv* env, jobject obj, jstring mssfile, jstring name, jstring version)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return;
	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return;
	const char* version_cstr = env->GetStringUTFChars(version, NULL);
	if (NULL == version_cstr) return;

	try {
		bspTool->set_driver_version(mssfile_cstr, name_cstr, version_cstr);
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);
	env->ReleaseStringUTFChars(name, name_cstr);
	env->ReleaseStringUTFChars(version, version_cstr);
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getLibraryList
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getLibraryList
  (JNIEnv* env, jobject obj, jstring mssfile)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return NULL;

	jstring ret = NULL;

	try {
		nlohmann::json doc = bspTool->get_libraries(mssfile_cstr);
		if (NULL == doc) return NULL;
		ret = env->NewStringUTF(doc.dump().c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);

	return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getLibrary
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getLibrary
  (JNIEnv* env, jobject obj, jstring mssfile, jstring name)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return NULL;
	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return NULL;

	jstring ret = NULL;

	try {
		nlohmann::json doc = bspTool->get_library(mssfile_cstr, name_cstr);
		if (NULL == doc) return NULL;
		ret = env->NewStringUTF(doc.dump().c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);
	env->ReleaseStringUTFChars(name, name_cstr);

	return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    removeLibrary
 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_removeLibrary
  (JNIEnv* env, jobject obj, jstring mssfile, jstring name)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return;
	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return;

	try {
		bspTool->delete_library(mssfile_cstr, name_cstr);
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);
	env->ReleaseStringUTFChars(name, name_cstr);
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    addLibrary
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_addLibrary
  (JNIEnv* env, jobject obj, jstring mssfile, jstring name, jstring version)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return;
	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return;
	const char* version_cstr = env->GetStringUTFChars(version, NULL);
	if (NULL == version_cstr) return;

	try {
		bspTool->add_library(mssfile_cstr, name_cstr, version_cstr);
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);
	env->ReleaseStringUTFChars(name, name_cstr);
	env->ReleaseStringUTFChars(version, version_cstr);
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    setLibraryVersion
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_setLibraryVersion
  (JNIEnv* env, jobject obj, jstring mssfile, jstring name, jstring version)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return;
	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return;
	const char* version_cstr = env->GetStringUTFChars(version, NULL);
	if (NULL == version_cstr) return;

	try {
		bspTool->set_driver_version(mssfile_cstr, name_cstr, version_cstr);
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);
	env->ReleaseStringUTFChars(name, name_cstr);
	env->ReleaseStringUTFChars(version, version_cstr);
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    generteBsp
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_generteBsp
  (JNIEnv* env, jobject obj, jstring hpf, jstring mssfile, jstring bsp_loc)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return;

	const char* hpf_cstr = env->GetStringUTFChars(hpf, NULL);
	if (NULL == hpf_cstr) return;
	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return;
	const char* bsp_loc_cstr = env->GetStringUTFChars(bsp_loc, NULL);
	if (NULL == bsp_loc_cstr) return;

	try {
		bspTool->generate_bsp_sources(hpf_cstr, mssfile_cstr, bsp_loc_cstr);
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);
	env->ReleaseStringUTFChars(hpf, hpf_cstr);
	env->ReleaseStringUTFChars(bsp_loc, bsp_loc_cstr);
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    regenerateBsp
 * Signature: (Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_regenerateBsp
  (JNIEnv* env, jobject obj, jstring mssfile, jstring bsp_loc)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return;
	const char* bsp_loc_cstr = env->GetStringUTFChars(bsp_loc, NULL);
	if (NULL == bsp_loc_cstr) return;

	try {
		bspTool->regenerate_bsp_sources(mssfile_cstr, bsp_loc_cstr);
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);
	env->ReleaseStringUTFChars(bsp_loc, bsp_loc_cstr);
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getAppNames
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getAppNames
  (JNIEnv* env, jobject obj)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	jstring ret = NULL;

	try {
		std::vector<std::string> app_names = bspTool->get_app_names();
		if (app_names.empty()) return NULL;
		std::string app_names_str = bspTool->vec_to_str(app_names);
		ret = env->NewStringUTF(app_names_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }
	
    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getStandaloneAppNames
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getStandaloneAppNames
  (JNIEnv* env, jobject obj)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	jstring ret = NULL;

	try {
		std::vector<std::string> app_names = bspTool->get_standalone_app_names();
		if (app_names.empty()) return NULL;
		std::string app_names_str = bspTool->vec_to_str(app_names);
		ret = env->NewStringUTF(app_names_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }
	
    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getFreeRtosAppNames
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getFreeRtosAppNames
  (JNIEnv* env, jobject obj)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	jstring ret = NULL;

	try {
		std::vector<std::string> app_names = bspTool->get_freertos_app_names();
		if (app_names.empty()) return NULL;
		std::string app_names_str = bspTool->vec_to_str(app_names);
		ret = env->NewStringUTF(app_names_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }
	
    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getRtthreadAppNames
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getRtthreadAppNames
  (JNIEnv* env, jobject obj)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	jstring ret = NULL;
	
	try {
		std::vector<std::string> app_names = bspTool->get_rtthread_app_names();
		if (app_names.empty()) return NULL;
		std::string app_names_str = bspTool->vec_to_str(app_names);
		ret = env->NewStringUTF(app_names_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }
	
    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getLinuxAppNames
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getLinuxAppNames
  (JNIEnv* env, jobject obj)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	jstring ret = NULL;
	
	try {
		std::vector<std::string> app_names = bspTool->get_linux_app_names();
		if (app_names.empty()) return NULL;
		std::string app_names_str = bspTool->vec_to_str(app_names);
		ret = env->NewStringUTF(app_names_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }
	
    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getAppDesc
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getAppDesc
  (JNIEnv* env, jobject obj, jstring name)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return NULL;

	jstring ret = NULL;

	try {
		std::string app_desc = bspTool->get_app_desc(name_cstr);
		if (app_desc.empty()) return NULL;
		ret = env->NewStringUTF(app_desc.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(name, name_cstr);

    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getAppLibs
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getAppLibs
  (JNIEnv* env, jobject obj, jstring name)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return NULL;

	jstring ret = NULL;

	try {
		std::vector<std::string> app_libs = bspTool->get_app_libs(name_cstr);
		if (app_libs.empty()) return NULL;
		std::string app_libs_str = bspTool->vec_to_str(app_libs);
		ret = env->NewStringUTF(app_libs_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }
	
	env->ReleaseStringUTFChars(name, name_cstr);

    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getAppExtraLibs
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getAppExtraLibs
  (JNIEnv* env, jobject obj, jstring mssfile, jstring name)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* mssfile_cstr = env->GetStringUTFChars(mssfile, NULL);
	if (NULL == mssfile_cstr) return NULL;
	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return NULL;

	jstring ret = NULL;

	try {
		std::vector<std::string> app_libs = bspTool->get_app_extra_libs(mssfile_cstr, name_cstr);
		if (app_libs.empty()) return NULL;
		std::string app_libs_str = bspTool->vec_to_str(app_libs);
		ret = env->NewStringUTF(app_libs_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(mssfile, mssfile_cstr);
	env->ReleaseStringUTFChars(name, name_cstr);

    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getAppBuildProperties
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getAppBuildProperties
  (JNIEnv* env, jobject obj, jstring name)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return NULL;

	jstring ret = NULL;

	try {
		std::map<std::string, std::string> app_props = bspTool->get_app_build_properties(name_cstr);
		if (app_props.empty()) return NULL;
		std::string app_props_str = bspTool->map_to_str(app_props);
		ret = env->NewStringUTF(app_props_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(name, name_cstr);

    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    generateApp
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_generateApp
  (JNIEnv* env, jobject obj, jstring bsp_loc, jstring name, jstring app_loc)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return;

	const char* bsp_loc_cstr = env->GetStringUTFChars(bsp_loc, NULL);
	if (NULL == bsp_loc_cstr) return;
	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return;
	const char* app_loc_cstr = env->GetStringUTFChars(app_loc, NULL);
	if (NULL == app_loc_cstr) return;

	try {
		bspTool->generate_app_sources(bsp_loc_cstr, name_cstr, app_loc_cstr);
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(name, bsp_loc_cstr);
	env->ReleaseStringUTFChars(name, name_cstr);
	env->ReleaseStringUTFChars(name, app_loc_cstr);
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getDriverAppNames
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getDriverAppNames
  (JNIEnv* env, jobject obj, jstring driver_name)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* driver_name_cstr = env->GetStringUTFChars(driver_name, NULL);
	if (NULL == driver_name_cstr) return NULL;

	jstring ret = NULL;

	try {
		std::vector<std::string> driver_app_names = bspTool->get_driver_app_names(driver_name_cstr);
		if (driver_app_names.empty()) return NULL;
		std::string driver_app_names_str = bspTool->vec_to_str(driver_app_names);
		ret = env->NewStringUTF(driver_app_names_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(driver_name, driver_name_cstr);

    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getChipFromHpf
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getChipFromHpf
  (JNIEnv* env, jobject obj, jstring hpf)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* hpf_cstr = env->GetStringUTFChars(hpf, NULL);
	if (NULL == hpf_cstr) return NULL;

	jstring ret = NULL;

	try {
		std::string chip = bspTool->get_chip_from_hpf(hpf_cstr);
		if (chip.empty()) return NULL;
		ret = env->NewStringUTF(chip.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(hpf, hpf_cstr);

    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getIncludePaths
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getIncludePaths
  (JNIEnv* env, jobject obj, jstring bsp_loc)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* bsp_loc_cstr = env->GetStringUTFChars(bsp_loc, NULL);
	if (NULL == bsp_loc_cstr) return NULL;

	jstring ret = NULL;

	try {
		std::vector<std::string> includes = bspTool->get_include_paths(bsp_loc_cstr);
		if (includes.empty()) return NULL;
		std::string includes_str = bspTool->vec_to_str(includes);
		ret = env->NewStringUTF(includes_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(bsp_loc, bsp_loc_cstr);

    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getLinkScripts
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getLinkScripts
  (JNIEnv* env, jobject obj, jstring app_loc, jstring chip, jstring download)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* app_loc_cstr = env->GetStringUTFChars(app_loc, NULL);
	if (NULL == app_loc_cstr) return NULL;
	const char* chip_cstr = env->GetStringUTFChars(chip, NULL);
	if (NULL == chip_cstr) return NULL;
	const char* download_cstr = env->GetStringUTFChars(download, NULL);
	if (NULL == download_cstr) return NULL;
	
	jstring ret = NULL;

	try {
		std::vector<std::string> link_scripts = bspTool->get_link_scripts(app_loc_cstr, chip_cstr, download_cstr);
		if (link_scripts.empty()) return NULL;
		std::string link_scripts_str = bspTool->vec_to_str(link_scripts);
		ret = env->NewStringUTF(link_scripts_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(app_loc, app_loc_cstr);
	env->ReleaseStringUTFChars(chip, chip_cstr);
	env->ReleaseStringUTFChars(download, download_cstr);

    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    updateBspHpf
 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_updateBspHpf
  (JNIEnv* env, jobject obj, jstring bsp_loc, jstring hpf)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return;

	const char* bsp_loc_cstr = env->GetStringUTFChars(bsp_loc, NULL);
	if (NULL == bsp_loc_cstr) return;
	const char* hpf_cstr = env->GetStringUTFChars(hpf, NULL);
	if (NULL == hpf_cstr) return;

	try {
		bspTool->update_bsp_hpf(bsp_loc_cstr, hpf_cstr);
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(bsp_loc, bsp_loc_cstr);
	env->ReleaseStringUTFChars(hpf, hpf_cstr);
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getOsNames
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getOsNames
  (JNIEnv* env, jobject obj)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	jstring ret = NULL;

	try {
		std::vector<std::string> os_names = bspTool->get_os_names();
		if (os_names.empty()) return NULL;
		std::string os_names_str = bspTool->vec_to_str(os_names);
		ret = env->NewStringUTF(os_names_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getDriverNames
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getDriverNames
  (JNIEnv* env, jobject obj)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	jstring ret = NULL;

	try {
		std::vector<std::string> driver_names = bspTool->get_driver_names();
		if (driver_names.empty()) return NULL;
		std::string driver_names_str = bspTool->vec_to_str(driver_names);
		ret = env->NewStringUTF(driver_names_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

    return ret;
}
/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getLibraryNames
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getLibraryNames
  (JNIEnv* env, jobject obj)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	jstring ret = NULL;

	try {
		std::vector<std::string> lib_names = bspTool->get_library_names();
		if (lib_names.empty()) return NULL;
		std::string lib_names_str = bspTool->vec_to_str(lib_names);
		ret = env->NewStringUTF(lib_names_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getOsVersions
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getOsVersions
  (JNIEnv* env, jobject obj, jstring name)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return NULL;

	jstring ret = NULL;

	try {
		std::vector<std::string> os_versions = bspTool->get_os_versions(name_cstr);
		if (os_versions.empty()) return NULL;
		std::string os_versions_str = bspTool->vec_to_str(os_versions);
		ret = env->NewStringUTF(os_versions_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(name, name_cstr);

    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getDriverVersions
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getDriverVersions
  (JNIEnv* env, jobject obj, jstring name)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return NULL;

	jstring ret = NULL;

	try {
		std::vector<std::string> driver_versions = bspTool->get_driver_versions(name_cstr);
		if (driver_versions.empty()) return NULL;
		std::string driver_versions_str = bspTool->vec_to_str(driver_versions);
		ret = env->NewStringUTF(driver_versions_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(name, name_cstr);

    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getLibraryVersions
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getLibraryVersions
  (JNIEnv* env, jobject obj, jstring name)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return NULL;

	jstring ret = NULL;

	try {
		std::vector<std::string> lib_versions = bspTool->get_driver_versions(name_cstr);
		if (lib_versions.empty()) return NULL;
		std::string lib_versions_str = bspTool->vec_to_str(lib_versions);
		ret = env->NewStringUTF(lib_versions_str.c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(name, name_cstr);

    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getOsFromCfgFile
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getOsFromCfgFile
  (JNIEnv* env, jobject obj, jstring name, jstring version)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return NULL;
	const char* version_cstr = env->GetStringUTFChars(version, NULL);
	if (NULL == version_cstr) return NULL;

	jstring ret = NULL;

	try {
		nlohmann::json doc = bspTool->get_os_from_cfgfile(name_cstr, version_cstr);
		if (NULL == doc) return NULL;
		ret = env->NewStringUTF(doc.dump().c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(name, name_cstr);
	env->ReleaseStringUTFChars(version, version_cstr);

    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getDriverFromCfgFile
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getDriverFromCfgFile
  (JNIEnv* env, jobject obj, jstring name, jstring version)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return NULL;
	const char* version_cstr = env->GetStringUTFChars(version, NULL);
	if (NULL == version_cstr) return NULL;

	jstring ret = NULL;

	try {
		nlohmann::json doc = bspTool->get_driver_from_cfgfile(name_cstr, version_cstr);
		if (NULL == doc) return NULL;
		ret = env->NewStringUTF(doc.dump().c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(name, name_cstr);
	env->ReleaseStringUTFChars(version, version_cstr);

    return ret;
}

/*
 * Class:     com_anlogic_sdk_tools_nativetool_NativeBspTool
 * Method:    getLibraryFromCfgFile
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_anlogic_sdk_tools_nativetool_NativeBspTool_getLibraryFromCfgFile
  (JNIEnv* env, jobject obj, jstring name, jstring version)
{
	bspTool = get_bsp_tool(env, obj);
	if (NULL == bspTool) return NULL;

	const char* name_cstr = env->GetStringUTFChars(name, NULL);
	if (NULL == name_cstr) return NULL;
	const char* version_cstr = env->GetStringUTFChars(version, NULL);
	if (NULL == version_cstr) return NULL;

	jstring ret = NULL;

	try {
		nlohmann::json doc = bspTool->get_library_from_cfgfile(name_cstr, version_cstr);
		if (NULL == doc) return NULL;
		ret = env->NewStringUTF(doc.dump().c_str());
	} catch(std::exception& e) {
        std::cout << "Error run function " << get_func_name(__func__) << ":\n" << e.what() << std::endl;
    }

	env->ReleaseStringUTFChars(name, name_cstr);
	env->ReleaseStringUTFChars(version, version_cstr);

    return ret;
}
